package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.mapper.MemberVisitMapper;
import com.zzyl.nursing.service.IMemberVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;





/**
 * TODO
 *
 * @Author XS
 * @Date 2025/10/22 22:20
 */
@Service
public class MemberVisitServiceImpl extends ServiceImpl<MemberVisitMapper, Reservation> implements IMemberVisitService {

    @Autowired
    private MemberVisitMapper memberVisitMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Override
    public int getCount() {
        Long userId = UserThreadLocal.getUserId();
        String phone = familyMemberMapper.selectById(userId).getPhone();


        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        Long count = this.lambdaQuery()
                .eq(Reservation::getMobile, phone)
                .between(Reservation::getCreateTime, startTime, endTime)
                .count();

//    int count = memberVisitMapper.getCount(userId,startTime,endTime);
        return Math.toIntExact(count);
    }


    @Override

    public void saveReservation(Reservation reservation) {
        reservation.setCreateTime(LocalDateTime.now());
    reservation.setStatus(0);

        this.save(reservation);
    }
}
